package main

import (
	"fmt"
	"net"
	"net/rpc"
	common "projRoot/commonLib"
)

func main() {
	// 首先是通过rpc.Dial拨号RPC服务，然后通过client.Call调用具体的RPC方法
	TCP_con, err := net.Dial("tcp", fmt.Sprintf(":%d", common.ServerPort))
	if err != nil {
		panic(err)
	}
	defer TCP_con.Close()
	client := rpc.NewClient(TCP_con)

	_, client_portID, _ := net.SplitHostPort(TCP_con.LocalAddr().String())
	_, server_portID, _ := net.SplitHostPort(TCP_con.RemoteAddr().String())

	fmt.Printf("已TCP连接服务器, 本地端口号%s, 服务器端口号:%s\n",
		client_portID, server_portID)

	result := common.Result{}
	args := common.Args{A: 35, B: 45}
	err = client.Call(fmt.Sprintf("%s.Add", common.ServiceName), args, &result)
	if err != nil {
		fmt.Println("result报文格式错误")
		panic(err)
	}
	fmt.Println("RPC已返回加法结果", result.Value)

	err = client.Call(fmt.Sprintf("%s.Divide", common.ServiceName), args, &result)
	if err != nil {
		panic(err)
	}
	fmt.Println("RPC已返回加法结果", result.Value)
}
